package com.cjw.campus.system.controller;

import com.baidu.ueditor.ActionEnter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjw.campus.system.entity.SchoolTable;
import com.cjw.campus.system.service.ISchoolTableService;
import core.MyStorageManager;
import core.result.JsonResult;
import core.utils.UploadUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 学校表 前端控制器
 * </p>
 *
 * @author cjw
 * @since 2022-11-16
 */
@RestController
@RequestMapping("/system/schoolTable")
@Api(tags = "学校管理", value = "用户管理接口")
public class SchoolTableController {

    @Autowired
    private ISchoolTableService schoolTableService;

    @GetMapping("/list")
    @ApiOperation(value = "查询所有学校信息", notes = "查询所有学校信息", httpMethod = "GET")
    public JsonResult list () {
        List<SchoolTable> list = schoolTableService.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据id查询学校信息", notes = "根据id查询学校信息", httpMethod = "GET")
    @ApiParam(name = "schoolId", value = "学校ID", required = true)
    public JsonResult getById (Integer schoolId) {
        SchoolTable school = schoolTableService.getById(schoolId);
        return JsonResult.ok(school);
    }

    @GetMapping("/getSchoolPage")
    @ApiOperation(value = "分页查询学校信息", notes = "分页查询学校信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2")
    })
    public JsonResult getSchoolPage (Integer currentPage,Integer pageSize) {
        IPage<SchoolTable> page = new Page<>(currentPage, pageSize);
        IPage<SchoolTable> schoolPage = schoolTableService.page(page);
        return JsonResult.ok(schoolPage);
    }

    @GetMapping("/getBySchoolPage")
    @ApiOperation(value = "模糊分页查询学校信息", notes = "模糊分页查询学校信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "Integer", defaultValue = "2"),
            @ApiImplicitParam(name = "schoolCode", value = "学校编码", dataType = "String"),
            @ApiImplicitParam(name = "schoolName", value = "学校名称", dataType = "String"),
    })
    public JsonResult getByBackgroundUserPage (Integer currentPage,Integer pageSize,String schoolCode, String schoolName) {
        IPage<SchoolTable> page = new Page<>(currentPage, pageSize);
        // QueryWrapper MybatisPlus 条件构造器
        QueryWrapper<SchoolTable> queryWrapper = new QueryWrapper<>();
        // lambda 表达式
        queryWrapper.lambda().like(SchoolTable::getSchoolCode, schoolCode);
        queryWrapper.lambda().like(SchoolTable::getSchoolName, schoolName);
        IPage<SchoolTable> mapPage = schoolTableService.page(page, queryWrapper);
        return JsonResult.ok(mapPage);
    }

    @PostMapping  ("/save")
    @ApiOperation(value = "保存学校信息", notes = "保存学校信息", httpMethod = "POST")
    @ApiParam(name = "schoolTable", value = "学校实体类", required = true)
    public JsonResult save (SchoolTable schoolTable) {
        // 保存用户信息
        boolean save = schoolTableService.save(schoolTable);
        // 判断用户是否新增成功
        if (save) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("新增失败");
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id修学校信息", notes = "根据id更新学校信息", httpMethod = "POST")
    @ApiParam(name = "schoolTable", value = "学校实体类", required = true)
    public JsonResult updateById (SchoolTable schoolTable) {
        boolean updateById = schoolTableService.updateById(schoolTable);
        // 判断用户是否修改成功
        if (updateById) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("修改失败");
    }

    @GetMapping("/removeById")
    @ApiOperation(value = "根据id删除学校信息", notes = "根据id删除学校信息", httpMethod = "GET")
    @ApiParam(name = "schoolId", value = "学校ID", required = true)
    public JsonResult removeById (Long schoolId) {
        boolean removeSchoolId = schoolTableService.removeById(schoolId);
//         判断用户是否删除成功
        if (removeSchoolId) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @PostMapping ("/removeByIds")
    @ApiOperation(value = "根据id批量删除学校信息", notes = "根据id批量删除学校信息", httpMethod = "POST")
    @ApiParam(name = "schoolId", value = "学校ID", required = true)
    public JsonResult removeByIds (Long[] idList) {
        boolean removeSchoolIds = schoolTableService.removeByIds(Arrays.asList(idList));
        if (removeSchoolIds) {
            // 成功返回
            return JsonResult.ok();
        }
        // 失败返回
        return JsonResult.error("删除失败");
    }

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    public JsonResult upload (@RequestPart("file") MultipartFile file, HttpServletRequest request) {
        String path = UploadUtil.uploadFile(file, request);
        return JsonResult.ok(path);
    }

    @Autowired
    private MyStorageManager storageManager;

    @RequestMapping("/uEditorServer")
    @ApiOperation(value = "百度富文本编辑器后端配置", notes = "百度富文本编辑器请求后端配置", httpMethod = "POST")
    public void uEditorServer(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "text/html");
        String rootPath = request.getServletContext().getRealPath("/");
        response.getWriter().write(new ActionEnter(storageManager, request, rootPath).exec());
    }

}
